package com.qst.rongxiaoserver.controller;

import com.qst.rongxiaoserver.common.JwtUtil;
import com.qst.rongxiaoserver.common.Result;
import com.qst.rongxiaoserver.model.AdminModel;
import com.qst.rongxiaoserver.model.dto.AdminLoginDto;
import com.qst.rongxiaoserver.service.IAdminService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
@Tag(name = "管理员管理", description = "管理员管理相关接口(服务层实现)")
public class AdminController {
    @Autowired
    IAdminService adminService;

    @PostMapping("/login")
    @Operation(summary = "管理员登录", description = "管理员登录接口(服务层实现)")
    public Result<AdminLoginDto> login(@Valid  @RequestBody AdminModel adminModel) {
        AdminLoginDto adminLoginDto = adminService.login(adminModel.getName(), adminModel.getPassword());
        if (adminLoginDto != null) {
            return Result.success(adminLoginDto, "登录成功");
        } else {
            return Result.fail("用户名或密码错误");
        }
    }

    @PostMapping("/add")
    @Operation(summary = "管理员添加", description = "管理员添加接口(服务层实现)")
    public Result addAdmin(@Valid @RequestBody AdminModel adminModel) {
        boolean saved = adminService.save(adminModel);
        if (saved) {
            return Result.success(null, "管理员添加成功");
        } else {
            return Result.fail("管理员添加失败");
        }
    }

    @PostMapping("/delete/{id}")
    @Operation(summary = "管理员删除", description = "管理员删除接口(服务层实现)")
    public Result deleteAdmin(@Valid @PathVariable Integer id) {
        boolean removed = adminService.removeById(id);
        if (removed) {
            return Result.success(null, "管理员删除成功");
        } else {
            return Result.fail("管理员删除失败");
        }
    }
}
